#!/usr/bin/perl
##########################################################################################################
## Author: Kjell R. Kjørstad                                                                            ##
## E-mail: kjell@kolug.org - for questions or comments.          					##
## File: This is a module created to search the channel log and return hits to users.			##
##	 This might be useful when bot is guarding a support channel. Still some work with this		##
##	 module, but it works so-so now.								##
##													##
## Purpose: Provide channel log search.								        ##
##	    												##
##           												##
## NOTE: If you do changes to existing code in any way, or have other comments an e-mail would be       ##
##	 highly appreciated.          	                        					##
##													##
## License:												##
##       This file is part of chanKeeper.								##
##													##
##       chanKeeper is free software: you can redistribute it and/or modify				##
##       it under the terms of the GNU General Public License as published by				##
##       the Free Software Foundation, either version 3 of the License, or				##
##       (at your option) any later version.								##
##													##
##       chanKeeper is distributed in the hope that it will be useful,					##
##       but WITHOUT ANY WARRANTY; without even the implied warranty of					##
##       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the					##
##       GNU General Public License for more details.							##
##   													##
##       You should have received a copy of the GNU General Public License				##
##       along with chanKeeper.  If not, see <http://www.gnu.org/licenses/>.				##
## 													##
## Copyright: Copyright © 2009, Kjell R. Kjørstad							##
##                                                                                                      ##
##########################################################################################################


package searchlogs;
use warnings;
use diagnostics;
use strict;
use POSIX qw(strftime);
use Time::Local qw( timegm );

sub search {
    my ($d,$m,$y) = (localtime())[3,4,5];
    my $starthere = strftime("%Y-%m-%d\n", gmtime(timegm(0,0,0,$d,$m,$y) - 168*60*60));	
    my $today = strftime("%Y-%m-%d\n", gmtime(timegm(0,0,0,$d,$m,$y) + 24*60*60));
    my $class = $_[0];
    my $dir = $_[1];
    my $starttime = $_[2] ||= $starthere;
    my $stoptime = $_[3] ||= $today;
    my @searchwords = splice(@_, 4);

    my @returntext;
    opendir DH, "$dir";
    #For each file in dir, read file and look for searchwords.
    foreach my $file ( readdir DH ) {
	#Skip file unless it's called *log* and is greater or equal to $starttime, and less or equal to $stoptime.
	next unless ( $file =~ /log/ and $file ge "$starttime" and $file le "$stoptime" );
	#Open the files, one at the time until there is no more files withing date range in directory.
	open(SEARCH,"$dir/$file");
	#While file is open, the loop will push lines which hit into @returntext.
	while ( <SEARCH> ) {
	    my $line = $_;
	    #If the search-string match in a line, return it.
	    if ($line =~ /@searchwords/i or $line =~ /$searchwords[0]/i and $line =~ /$searchwords[1]/i and $line =~ /$searchwords[2]/i and $line =~ /$searchwords[3]/i ) {
		push @returntext, ("From $file: $line");
	    }
	}
	#Close each file after read.
	close(SEARCH);
    }
    #If there are more than 20 hits undefine @returntext and push blabla into it.
    if ( @returntext > '20' ) { 
	undef @returntext;
	push @returntext, ("Your search returned over 20 hits, try to narrow your search.");
	return(@returntext);
    #If there are more than 0 hits undefine @returntext and push blabla into it.
    } elsif ( @returntext == '0' ) {
	undef @returntext;
	push @returntext, ("Your search returned 0 hits, try to widen your search.");
	return(@returntext);
    #If there are more than 0 hits and less than 20 hits,return @returntext.
    } else {
	return(@returntext);
    }
}

1;